<?php
//商铺管理
namespace App\Http\Controllers\App;
use App\Http\Controllers\App\BaseController as Controller;

use Illuminate\Http\Request;
use App\Models\Shop;
use App\Models\ShopInfo;

class ShopController extends Controller
{
    protected $thisLoginClerk;
    public function __construct(Request $request)
    {
        $checkThisClerk = $this->getShopUserID($request->token,$request->device_id);
        if(!$checkThisClerk){
            return show(2001,'无效token');
        }else{//判断用户角色
            $this->thisLoginClerk = $checkThisClerk;
        }
    }
    /**
     *      商家信息
     * {
        "id": 1,
        "mobile": "18819470669",
        "name": "我是好人",
        "sex": 0,
        "role": -1,
        "money": "111794.85",
        "point": 0,
        "avatar": "/storage/user_1/info/DZtKopSUpEurVEX38zfYPtLxdlnl024Urrq1FXRI.jpeg",
        "device_id": "235612361465623131",
        "shop_user_id": 1
        }
     *  服务员
        {
        "id": 1,
        "shop_id": 1,
        "name": "test",
        "mobile": "18819470669",
        "role": 1,
        "avatar": null,
        "sex": 1,
        "employeeId": "11jzqwditr",
        "status": 1,
        "device_id": "235612361465623131",
        "shop_user_id": "1",
        "rolename": "服务员"
        }
     */
    // 获取所有商家的信息
    public function showAllShops()
    {
        if ($this->thisLoginClerk->role == '-1'){
            $user_id = $this->thisLoginClerk->shop_user_id;
            $shop = Shop::where('user_id',$user_id)
                ->get(['id','shop_avatar','note','shop_name']);
            if ($shop->isNotEmpty()){
                foreach ($shop as $sh){
                    $shopinfo = ShopInfo::where('shop_id',$sh->id)->first();
                    if ($shopinfo){
                        $sh['is_open'] = $shopinfo->is_open;
                    }else{
                        $sh['is_open'] = 0;
                    }

                }
            }
            return show(200,'所有商家信息',$shop);
        }else{
            $s_id = $this->thisLoginClerk->shop_id;
            $shop = Shop::where('id',$s_id)
                ->get(['id','shop_avatar','note','shop_name']);
            if ($shop->isNotEmpty()){
                foreach ($shop as $sh){
                    $shopinfo = ShopInfo::where('shop_id',$sh->id)->first();
                    if ($shopinfo){
                        $sh['is_open'] = $shopinfo->is_open;
                    }else{
                        $sh['is_open'] = 0;
                    }

                }
            }
            return show(200,'所有商家信息',$shop);
        }

    }
}
